package com.other;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Java8_foreach_sorted {

	public static void main(String[] args) {
		Java8_foreach_sorted j = new Java8_foreach_sorted();
		j.test1();
		j.test2();
	}
	
	public void test1() {
		List<Integer> list = new ArrayList<>();
		list.add(5);
		list.add(2);
		list.add(9);
		list.add(7);
		list.add(3);
		list.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);
	}
	
	public void test2() {
		//测试数据，请不要纠结数据的严谨性
		List<Student> studentList = new ArrayList<>();
		studentList.add(new Student("李小明",18,1.76));
		studentList.add(new Student("张小丽",18,1.61));
		studentList.add(new Student("王大朋",19,1.82));
		studentList.add(new Student("陈小跑",17,1.67));
		
		System.out.println("1>>>>>>>>>>>>>>>>>>>>");
		//排序前输出
		Student.printStudents(studentList);
		//按年龄排序(Integer类型)
		List<Student> studentsSortName = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
		//排序后输出
		Student.printStudents(studentsSortName);
		System.out.println("1<<<<<<<<<<<<<<<<<<<<");
		
		System.out.println("2>>>>>>>>>>>>>>>>>>>>");
		//排序前输出
		Student.printStudents(studentList);
		//按年龄排序(Integer类型)
		List<Student> studentsSortName1 = studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
		//排序后输出
		Student.printStudents(studentsSortName1);
		System.out.println("2<<<<<<<<<<<<<<<<<<<<");
		
		System.out.println("3>>>>>>>>>>>>>>>>>>>>");
		//排序前输出
		Student.printStudents(studentList);
        //按年龄排序(Integer类型)
        List<Student> studentsSortName2 = studentList.stream()
                .sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getHeight))
                .collect(Collectors.toList());
        //排序后输出
        Student.printStudents(studentsSortName2);
        System.out.println("3<<<<<<<<<<<<<<<<<<<<");
	}
	
	public static class Student implements Comparable<Student> {

		//名称
	    private String name;

	    //年龄
	    private Integer age;

	    //身高
	    private Double height;
		
	    
		@Override
		public String toString() {
			return "Student [name=" + name + ", age=" + age + ", height=" + height + "]";
		}

		public static void printStudents(List<Student> students){
	        System.out.println("[姓名]\t\t[年龄]\t\t[身高]");
	        System.out.println("----------------------------------------------------------");
	        students.forEach(s->System.out.println(s.toString()));
	        System.out.println(" ");
	    }
		
		public Student(String name, Integer age, Double height) {
			this.name = name;
			this.age = age;
			this.height = height;
		}

		@Override
		public int compareTo(Student o) {
			return this.age.compareTo(o.getAge());
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getAge() {
			return age;
		}

		public void setAge(Integer age) {
			this.age = age;
		}

		public Double getHeight() {
			return height;
		}

		public void setHeight(Double height) {
			this.height = height;
		}
		
	}
}
